package com.kyc.kycxkplaygame.dbutils.IntImp;

import android.content.Context;
import com.kyc.kycxkplaygame.dbutils.inter.HttpApi;
import com.kyc.kycxkplaygame.dbutils.inter.HttpQuestResultListener;
import com.kyc.kycxkplaygame.dbutils.inter.IApi;
import com.kyc.kycxkplaygame.entity.*;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2017/11/1.
 */

public class AlasbusyIApi implements IApi {

    private Context context;
    /*
   * Singleton lazy initialization start
   */
    private static AlasbusyIApi instanceSingleton = null;

    /**
     * you can init some param in this struct
     */
    public AlasbusyIApi(Context context) {
        this.context = context;
    }

    /**
     * please init your context is getApplication()
     *
     * @param context
     * @return
     */
    public static AlasbusyIApi getInstance(Context context) {
        /*if (instanceSingleton == null) {
            instanceSingleton = new WalltaUser(context);
        }*/
        //因带了httpQuestResultListener等接口回调。故使用单列时。WalltaUser在其他地方instance get 时。会发现回调不成功
        instanceSingleton = new AlasbusyIApi(context);
        return instanceSingleton;
    }

    private HttpQuestResultListener httpQuestResultListener;
    private WeakHandler weakHandler;

    @Override
    public void setHttpQuestResultListener(HttpQuestResultListener httpQuestResultListener) {
        this.httpQuestResultListener = httpQuestResultListener;
    }

    @Override
    public void setWeakHanlder(WeakHandler hanlder) {
        this.weakHandler = hanlder;
    }

   /* @Override
    public void saveUserAcount(String userAcount) {
        PreferenceHelper.saveCurUserCount(context, userAcount);
    }

    @Override
    public String getUserAcount() {
        return PreferenceHelper.getCurUserCount(context);
    }

    @Override
    public void saveUserPasswd(String passwd) {
        PreferenceHelper.saveCurUserpwd(context, passwd);
    }

    @Override
    public String getUserPasswd() {
        return PreferenceHelper.getCurUserPwd(context);
    }

    @Override
    public Boolean getUserRegistionStatu() {
        return PreferenceHelper.getCurUsreRegistionState(context);

    }

    @Override
    public void saveUerRegistionStatu(Boolean regstate) {
        PreferenceHelper.saveCurUsreRegistionState(context, regstate);
        //如果用户登录成功。保存登录使用次数
        if (regstate) {
            setLoginUsredCount();
        }
    }

    @Override
    public int getLoginUsredCount() {
        String key = getUserAcount() + "_" + "LoginSucCount";
        int loginSucCount = PreferenceHelper.getIntValuewithField(context, key);

        return loginSucCount;
    }

    @Override
    public void setLoginUsredCount() {
        String key = getUserAcount() + "_" + "LoginSucCount";
        int loginSucCount = PreferenceHelper.getIntValuewithField(context, key);
        if (loginSucCount == 0) {
            loginSucCount = 1;
        } else if (loginSucCount > 0) {
            loginSucCount++;
        }
        PreferenceHelper.saveIntfieldtoSharePre(context, key, loginSucCount);
    }

    @Override
    public void saveUserNick(String usernick) {
        PreferenceHelper.saveCurUserNick(context, usernick);
    }

    @Override
    public String getUserNick() {
        return PreferenceHelper.getCurUserNick(context);
    }

    @Override
    public void saveUsername(String username) {

        PreferenceHelper.saveCurUserName(context, username);
    }

    @Override
    public String getUsername() {
        return PreferenceHelper.getCurUserName(context);
    }


    @Override
    public void saveUserSex(int sex) {
        PreferenceHelper.saveCurUserSex(context, sex);
    }

    @Override
    public int getUserSex() {
        return PreferenceHelper.getCurUserSex(context);
    }

    @Override
    public String getUserAccessToken() {
        String curUserCount = PreferenceHelper.getCurUserCount(context);
        String filed = curUserCount + "_" + "AccessToken";
        return PreferenceHelper.getValuewithField(context, filed);
    }

    @Override
    public void saveUserAccessTokne(String accessTokne) {
        String curUserCount = PreferenceHelper.getCurUserCount(context);
        String filed = curUserCount + "_" + "AccessToken";
        PreferenceHelper.savefieldtoSharePre(context, filed, accessTokne);
    }

    @Override
    public void saveUserHeaderUrl(String headerurl) {
        String curUserCount = PreferenceHelper.getCurUserCount(context);
        String filed = curUserCount + "_" + "headerurl";
        PreferenceHelper.savefieldtoSharePre(context, filed, headerurl);
    }

    @Override
    public String getUserHeaderUrl() {
        String curUserCount = PreferenceHelper.getCurUserCount(context);
        String filed = curUserCount + "_" + "headerurl";
        return PreferenceHelper.getValuewithField(context, filed);
    }*/


    @Override
    public void userLogin(String username, String userpwd) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.userLogin(username,userpwd)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<UserInfoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(UserInfoBean userInfoBean) {
                        if (httpQuestResultListener != null) {
                            if (userInfoBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = userInfoBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(userInfoBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(userInfoBean);
                            }

                        }
                    }
                });
    }

    @Override
    public void usersRegister(String username, String userpwd) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.usersRegister(username,userpwd)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<RegisterUserInfoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(RegisterUserInfoBean userInfoBean) {
                        if (httpQuestResultListener != null) {
                            if (userInfoBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = userInfoBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(userInfoBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(userInfoBean);
                            }
                        }
                    }
                });
    }

    @Override
    public void findUserName(String username) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.findUserName(username)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<findUserNameBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(findUserNameBean findUserNameBean) {
                        if (httpQuestResultListener != null) {
                            if (findUserNameBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = findUserNameBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(findUserNameBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(findUserNameBean);
                            }
                        }
                    }
                });
    }

    @Override
    public void insertProps(String username, String propname, int propnum) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.insertProps(username,propname,propnum)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<PropsCode>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(PropsCode propsCode) {
                        if (httpQuestResultListener != null) {
                            if (propsCode == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = propsCode.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(propsCode);
                            } else {
                                httpQuestResultListener.onQuestErroCode(propsCode);
                            }
                        }
                    }
                });
    }

    @Override
    public void findUserProp(String username) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.findUserProp(username)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<UsersProps>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(UsersProps usersProps) {
                        if (httpQuestResultListener != null) {
                            if (usersProps == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = usersProps.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(usersProps);
                            } else {
                                httpQuestResultListener.onQuestErroCode(usersProps);
                            }
                        }
                    }
                });
    }

    @Override
    public void insertLevel(UserLevelBean userLevelBean) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.insertLevel(userLevelBean)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<LevelCode>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(LevelCode levelCode) {
                        if (httpQuestResultListener != null) {
                            if (levelCode == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = levelCode.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(levelCode);
                            } else {
                                httpQuestResultListener.onQuestErroCode(levelCode);
                            }
                        }
                    }
                });
    }

    @Override
    public void updateUserLEvel(Long userID, int newLevel) {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.updateUserLEvel(userID,newLevel)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<LevelCode>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(LevelCode levelCode) {
                        if (httpQuestResultListener != null) {
                            if (levelCode == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = levelCode.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(levelCode);
                            } else {
                                httpQuestResultListener.onQuestErroCode(levelCode);
                            }
                        }
                    }
                });
    }

    @Override
    public void getShoppingProp() {
        HttpApi httpApi = RetrofitUtils.createApi(context, HttpApi.class);
        httpApi.getShoppingProp()
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<PropTypeInfoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(PropTypeInfoBean propTypeInfoBean) {
                        if (httpQuestResultListener != null) {
                            if (propTypeInfoBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = propTypeInfoBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(propTypeInfoBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(propTypeInfoBean);
                            }
                        }
                    }
                });
    }

    @Override
    public void getCommunityBlock(String s, String a) {
        HttpApi httpAPI = RetrofitUtils.createApi(context, HttpApi.class);
        httpAPI.getCommunityBlock(s, a)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(String blockEntity) {
                        if (httpQuestResultListener != null) {
                            if (blockEntity == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            /*int code = blockEntity.getResponseCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(blockEntity);
                            } else {
                                httpQuestResultListener.onQuestErroCode(code);
                            }*/

                        }
                    }
                });

    }

    @Override
    public void doAuthRoom(String s, String token) {
        HttpApi httpAPI = RetrofitUtils.createApi(context, HttpApi.class);
        httpAPI.doAuthRoom(s, token)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(String blockEntity) {
                        if (httpQuestResultListener != null) {
                            if (blockEntity == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            /*int code = blockEntity.getResponseCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(blockEntity);
                            } else {
                                httpQuestResultListener.onQuestErroCode(code);
                            }*/

                        }
                    }
                });
    }


   /* @Override
    public void uploadPictrue(String filepath) {
        HttpApi service = RetrofitUtils.createPrivApi(context, HttpApi.class, HttpApiConst.kycxkBaseDomain);
        File file = new File(filepath);
        if (!file.exists()) {
            return;
        }
        String filekey = "";
        Map<String, RequestBody> params = new HashMap<String, RequestBody>();
        RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);//image/png
        Log.i("TAG","fileBody ------  " +fileBody);
        filekey +="\":filename=\""+file.getName();
        params.put(*//*"image"*//*filekey, fileBody);

        service.testUploadPictrue(PreferenceHelper.getRandom(context),PreferenceHelper.getToken(context),params)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<UploadPicEntiry>() {
                    @Override
                    public void onCompleted() {
                        Log.i("TAG", "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TAG", "onError ----- " + e.getMessage());
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(UploadPicEntiry uploadPicEntiry) {
                        //{"data":[{"responseCode":200,"md5":"70a63990a761d577059ed6f9630f4d78"}]}
                        // http json  response:eg-{"data":[{"responseCode":200,"md5":"e93b80c7e6fc41cd921c823a5eade5b0"},{"responseCode":200,"md5":"70a63990a761d577059ed6f9630f4d78"}]}
                        if (httpQuestResultListener != null) {
                            if (uploadPicEntiry == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }else {
                                // List<UploadPicEntiry.Data> code = uploadPicEntiry.getData();
                                int code = uploadPicEntiry.getCode();
                                if (code == 200) {
                                    httpQuestResultListener.onSuccess(uploadPicEntiry);
                                } else {
                                    httpQuestResultListener.onQuestErroCode(uploadPicEntiry);
                                }
                            }
                        }
                    }
                });
    }*/

   /* @Override
    public void getAuthorized(String str, String sign) {
        HttpApi httpAPI = RetrofitUtils.createApi(context, HttpApi.class);
        httpAPI.getAuthorized(HttpApiConst.appkey, str,sign)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<AuthorizedBean>() {
                    @Override
                    public void onCompleted() {
                        Log.i("TAG","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(AuthorizedBean authorizedBean) {
                        if (httpQuestResultListener != null) {
                            if (authorizedBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = authorizedBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(authorizedBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(authorizedBean);
                            }

                        }
                    }
                });
    }

    @Override
    public void getVIPVistorList(int page) {
        HttpApi httpAPI = RetrofitUtils.createApi(context, HttpApi.class);
        httpAPI.getVIPVistorList("9be07cc3a370ce0ca407c10ab693a9b2",page)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<IsBlackBean>() {
                    @Override
                    public void onCompleted() {
                        Log.i("TAG","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(IsBlackBean isBlackBean) {
                        if (httpQuestResultListener != null) {
                            if (isBlackBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = isBlackBean.getResponseCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(isBlackBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(isBlackBean);
                            }

                        }
                    }
                });
    }

    @Override
    public void getLogin(String username, String password) {
        HttpApi httpAPI = RetrofitUtils.createApi(context, HttpApi.class);
        httpAPI.getLogin(PreferenceHelper.getToken(context),PreferenceHelper.getRandom(context),username,password)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onCompleted() {
                        Log.i("TAG","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if (httpQuestResultListener != null) {
                            if (loginBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = loginBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(loginBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(loginBean);
                            }

                        }
                    }
                });
    }

    @Override
    public void getUserRegister(String username, String password) {
        HttpApi httpAPI = RetrofitUtils.createApi(context, HttpApi.class);
        httpAPI.getUserRegister(PreferenceHelper.getToken(context),PreferenceHelper.getRandom(context),username,password)
                .subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
                .subscribe(new Observer<RegisterBean>() {
                    @Override
                    public void onCompleted() {
                        Log.i("TAG","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (httpQuestResultListener != null) {
                            httpQuestResultListener.onError(e);
                        }
                    }

                    @Override
                    public void onNext(RegisterBean authorizedBean) {
                        if (httpQuestResultListener != null) {
                            if (authorizedBean == null) {
                                httpQuestResultListener.onError(new Throwable());
                            }
                            int code = authorizedBean.getCode();
                            if (code == 200) {
                                httpQuestResultListener.onSuccess(authorizedBean);
                            } else {
                                httpQuestResultListener.onQuestErroCode(authorizedBean);
                            }

                        }
                    }
                });
    }*/

}
